<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    table {
      width: 100%;
      border-collapse: collapse;
    }

    table th {
      padding: 5px 0;
    }

    table td {
      padding: 3px 0;
    }

    .container {
      overflow: auto;
      width: 800px;
      margin: 50px auto;
    }
    .table-header-wrap th {
      border: 1px solid #ccc;
      background-color: #fafafa;
      color: #000000d9;
      font-weight: 500;
      transition: background .3s ease;
    }

    .table-body-wrap {
      overflow-y: auto;
      max-height: 300px;
      border-left: 1px solid #ccc;
      border-bottom: 1px solid #ccc;
    }

    /* 滚动条整体 */
    .table-body-wrap::-webkit-scrollbar {
        display: none;
    }

    .table-body-wrap td {
      border-bottom: 1px solid #ccc;
      border-right: 1px solid #ccc;
    }

    .table {
      table-layout: fixed;
    }
  </style>
</head>

<body>
  <div class="container">
    <div id="my-table">
      <div class="table-header-wrap">
        <table class="table my-table-header"></table>
      </div>
      <div class="table-body-wrap">
        <table class="table my-table-body"></table>
      </div>
    </div>
    <button id="render-data-btn">点击渲染表格数据</button>
  </div>

  <script src="./data.js"></script>
  <script src="./util.js"></script>
  <script src="./core.js"></script>
  <script src="./body-core.js"></script>
  <script>
    (function TableHeadMerge(win, doc, utils, parseHeader, BodyCore) {
      const { renderTable, createElementAutoScroll } = utils;
      const columns = parseHeader(header); // 序列化表头
      const render = renderTable('#my-table', columns, header);
      // 模拟数据
      const mockData = (total) => {
        return new Array(total || 20).fill(null).map((itm, idx) => {
          return {
            id: Date.now(),
            idx: idx + 1,
            userInfo: '',
            homeAddress: '',
            name: "张三",
            province: "四川省",
            city: "成都市",
            county: "成华区",
            address: "猛追湾街道270号（成都339）",
            remark: '备注信息',
            remark_name: '张三',
            remark_content: '111',
            remark_date: '2022-10-18',
            create_time: '2022-10-18'
          }
        })
      }

      // 点击渲染
      doc.querySelector('#render-data-btn').addEventListener('click', () => {
        const data = mockData(30);
        const autoScroll = createElementAutoScroll('.table-body-wrap', { speed: 30, isLoop: true});
        const body = BodyCore.create(header, data)
        render.renderBody(body.renderColumns, data, body.cellMerge); // 渲染表体
        autoScroll.start(); // 自动滚动
      }, false)
    })(window, document, window.utils, window.parseHeaderData, window.BodyCore);
  </script>
</body>

</html>